TermList<PElemsType>

type definition:

type TermList<PElemsT extends PDataRepresentable> = Term<PList<PElemsT>> & {

    readonly head: UtilityTermOf<PElemsT>

    readonly tail: TermList<PElemsT>

    readonly length: TermInt

    readonly atTerm:        TermFn<[PInt], PElemsT>
    readonly at:            ( index: Term<PInt> ) => UtilityTermOf<PElemsT> 
    
    readonly findTerm:      TermFn<[PLam<PElemsT,PBool>], PMaybeT<PElemsT>>
    readonly find:          ( predicate: Term<PLam<PElemsT,PBool>> ) => Term<PMaybeT<PElemsT>>

    readonly filterTerm:    TermFn<[PLam<PElemsT,PBool>], PList<PElemsT>>
    readonly filter:        ( predicate: Term<PLam<PElemsT,PBool>> ) => TermList<PElemsT>

    readonly preprendTerm:  TermFn<[PElemsT], PList<PElemsT>>
    readonly preprend:      ( elem: Term<PElemsT> ) => TermList<PElemsT>
    
    readonly mapTerm: <ResultT extends ConstantableTermType>( resultT: ResultT ) =>
        TermFn<[PLam<PElemsT, ToPType<ResultT>>], PList<ToPType<ResultT>>>
    readonly map:     <PResultElemT extends PType>( f: Term<PLam<PElemsT,PResultElemT>> ) => 
        TermList<PResultElemT>

    readonly everyTerm: TermFn<[PLam<PElemsT, PBool>], PBool>
    readonly every:     ( predicate: Term<PLam<PElemsT, PBool>> ) => TermBool
    
    readonly someTerm:  TermFn<[PLam<PElemsT, PBool>], PBool>
    readonly some:      ( predicate: Term<PLam<PElemsT, PBool>> ) => TermBool

}

NOTE most of the equivalent expressions and some of the terms that requre some other informations are plu-ts generics

What is UtilityTermOf?

TermList is a generic, and it works for every PType

However, given a generic PType we don't know what is its utility term or even if it has any

UtilityTermOf handles all that; if PElemsT is something that can have an utility term it returns that utility term; otherwise returns the plain term.

example

UtilityTermOf<PByteString> === TermBS

UtilityTermOf<PDelayed<PByteString>> === Term<PDelayed<PByteString>>

returns: UtilityTermOf<PElemsT>

throws if the list is empty ([])

equivalent expression:

phead( elemsT ).$( term )

returns the first element of the list

tail

returns: UtilityTermOf<PElemsT>

throws if the list is empty ([])

equivalent expression:

ptail( elemsT ).$( term )

returns a new list with the same elements of the term except for the first one.

length

returns: TermInt

equivalent expression:

plength( elemsT ).$( term )

O(n)

returns the number of elements present in the list.

at

parameter: index type: Term<PInt>

returns: UtilityTermOf<PElemsT>

throws if index >= length

equivalent expression:

pindexList( elemsT ).$( term ).$( index )

returns the element at position index.

find

parameter: predicate type: Term<PLam<PElemsT,PBool>>

returns: Term<PMaybeT<PElemsT>>

equivalent expression:

pfind( elemsT ).$( predicate ).$( term )

returns PMaybe( elemsT ).Just({ val: elem }) where elem is the first element of the list that satisfies the predicate; returns PMaybe( elemsT ).Nothing({}) if none of the elements satisfies the predicate.

filter

parameter: predicate type: Term<PLam<PElemsT,PBool>>

returns: TermList<PElemsT>

equivalent expression:

pfilter( elemsT ).$( predicate ).$( term )

returns a new list containing only the elements that satisfy the predicate.

prepend

parameter: elem type: Term<PElemsT>

returns: TermList<PElemsT>

equivalent expression:

pprepend( elemsT ).$( elem ).$( term )

returns a new list with the elem element added at the start of the list.

map

parameter: f type: Term<PLam<PElemsT,PResultElemT>>

returns: TermList<PResultElemT>

equivalent expression:

pmap( elemsT, resultT ).$( f ).$( term )

returns a new list containing the result of applying f to the element in the same position.

NOTE mapTerm requires the return type of f; this is not true for map because map can understand the type directly from the parameter f.

every

parameter: predicate type: Term<PLam<PElemsT, PBool>>

returns: TermBool

equivalent expression:

pevery( elemsT ).$( predicate ).$( list )

applies the predicate to each term of the list and returns pBool( false ) if any of them is pBool( false ); pBool( true ) otherwise;

some

parameter: predicate type: Term<PLam<PElemsT, PBool>>

returns: TermBool

equivalent expression:

psome( elemsT ).$( predicate ).$( list )

applies the predicate to each term of the list and returns pBool( true ) if any of them is pBool( true ); pBool( false ) otherwise;